package cda.pager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.widget.Toast;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Pager extends Activity{
    
    private static Handler hand;
    
    public static int                       SCANNER = 1;
    public static int                       AFFICHE_CODE = 2;
    public static int                       CREER_CARTE = 3;
    
    private Intent                          intent;
    private static Context                  context;
    
    private GestionFichier                  gestionF;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gestionF = new GestionFichier();
        
        context = getApplicationContext();
        
        try {
            gestionF.verifFichier();
        } catch (IOException ex) {
            Logger.getLogger(Pager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        hand = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch(msg.what){
                    case 1 :
                        intent = new Intent("com.google.zxing.client.android.SCAN");
                        startActivityForResult(intent, 0);
                        break;
                    case 2 :
                        intent = new Intent(getApplicationContext(), AfficheCode.class);
                        startActivity(intent);
                        break;
                    case 3 :
                        intent = new Intent(getApplicationContext(), CreerCarteFidelite.class);
                        startActivity(intent);
                        
                        break;
                } 
            }
        };
                
        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.mypager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);   
    }
    
    @Override
    public void onResume(){
        super.onResume();
    }
    
    public static void startAct(int type){
        hand.sendEmptyMessage(type);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                MyPagerAdapter.DONNEES = intent.getStringExtra("SCAN_RESULT");
                MyPagerAdapter.TYPE = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Toast.makeText(this, MyPagerAdapter.TYPE, Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Echec", Toast.LENGTH_LONG).show();
            }
        }
    }
    
    public static Context getContext(){
        return context;
    }
}
